<template>
  <a-card :bordered='false'>
    <!-- 查询区域 -->
    <div class='table-page-search-wrapper'>
      <a-form layout='inline' @keyup.enter.native='searchQuery'>
        <a-row :gutter='24'>
          <a-col :span='6'>
            <a-form-model-item label='年级' prop='queryParam.gradeId'>
              <j-dict-select-tag @change='chgGradeId' v-model='queryParam.gradeId' placeholder='请选择年级'
                                 dictCode='cou_grade,grade_name,id,tenant_id  is not null order by  grade_order' />
            </a-form-model-item>
          </a-col>
          <a-col :span='6'>
            <a-form-model-item label='班级' prop='queryParam.classId'>
              <j-dict-select-tag v-model='queryParam.classId' placeholder='请选择班级'
                                 :dictCode='queryParamSelect.classIdSelect' />
            </a-form-model-item>
          </a-col>
          <a-col :span='6'>
            <a-form-model-item label='考试' prop='queryParam.examinationId'>
              <j-dict-select-tag @change='chgExaminationId' v-model='queryParam.examinationId'
                                 placeholder='请选择考试'
                                 :dictCode='queryParamSelect.examinationIdSelect' />
            </a-form-model-item>
          </a-col>
          <a-col :span='6'>
            <a-form-item label='试卷'>
              <j-dict-select-tag v-model='queryParam.examinationPaperId'
                                 placeholder='请选择试卷' :dictCode='queryParamSelect.examinationPaperIdSelect' />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :offset='8'>
            <h5 style='color: red'>点击导入文件之前，请先选择导入文件对应的考试、班级、试卷。</h5>
            <h5 style='color: red'>Excel文件请使用xlsx格式文件,如果不是请使用wps或者office另存一下修改格式后进行导入。</h5>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class='table-operator'>
      <a-row>
        <a-col :offset='10'>
          <a-upload name='file' :showUploadList='false' :multiple='false' :headers='tokenHeader'
                    :action='importExcelUrl'
                    @change='handleImportExcel'>
            <a-button type='primary' icon='import' @click='openModal'>导入</a-button>
          </a-upload>
          <a-button type='primary' icon='search' @click='this.loadData'>查询</a-button>
          <a-button type='primary' icon='plus' @click='handleOpenSubmit'>解析</a-button>
          <!-- 高级查询区域 -->
          <!--      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
          <a-dropdown v-if='selectedRowKeys.length > 0'>
            <a-menu slot='overlay'>
              <a-menu-item key='1' @click='batchDel'>
                <a-icon type='delete' />
                删除
              </a-menu-item>
            </a-menu>
            <a-button style='margin-left: 8px'> 批量操作
              <a-icon type='down' />
            </a-button>
          </a-dropdown>
          <template>
        <span style="float:right;">
          <a @click="searchQuery()"><a-icon type="sync" />刷新</a>
          <a-divider type="vertical" />
          <a-button v-print="'#datatable'" ghost type="primary">打印</a-button>
        </span>
          </template>
        </a-col>
      </a-row>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class='ant-alert ant-alert-info' style='margin-bottom: 16px;'>
        <i class='anticon anticon-info-circle ant-alert-icon'></i> 已选择 <a
        style='font-weight: 600'>{{ selectedRowKeys.length }}</a>项
        <a style='margin-left: 24px' @click='onClearSelected'>清空</a>
      </div>

      <a-table
        id="datatable"
        ref='table'
        size='middle'
        :scroll='{x:true}'
        bordered
        rowKey='id'
        :columns='columns'
        :dataSource='dataSource'
        :pagination='ipagination'
        :loading='loading'
        :rowSelection='{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}'
        class='j-table-force-nowrap'
        @change='handleTableChange'>

        <template slot='htmlSlot' slot-scope='text'>
          <div v-html='text'></div>
        </template>
        <template slot='imgSlot' slot-scope='text'>
          <span v-if='!text' style='font-size: 12px;font-style: italic;'>无图片</span>
          <img v-else :src='getImgView(text)' height='25px' alt=''
               style='max-width:80px;font-size: 12px;font-style: italic;' />
        </template>
        <template slot='fileSlot' slot-scope='text'>
          <span v-if='!text' style='font-size: 12px;font-style: italic;'>无文件</span>
          <a-button
            v-else
            :ghost='true'
            type='primary'
            icon='download'
            size='small'
            @click='downloadFile(text)'>
            下载
          </a-button>
        </template>

        <span slot='action' slot-scope='text, record'>
          <a @click='handleEdit(record)'>编辑</a>

          <a-divider type='vertical' />
          <a-dropdown>
            <a class='ant-dropdown-link'>更多 <a-icon type='down' /></a>
            <a-menu slot='overlay'>
              <a-menu-item>
                <a @click='handleDetail(record)'>详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title='确定删除吗?' @confirm='() => handleDelete(record.id)'>
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <import-excel-analysis-modal ref='modalForm' @ok='modalFormOk'></import-excel-analysis-modal>
    <JSelectClassExaminationSubmit ref='modalImportExcelAnalysisList'
                                   :title='this.title'
                                   :isMulti='this.isMulti'
                                   :msg='this.msg'
                                   @closeExaminationSubmit='handleCloseExaminationSubmit'
    ></JSelectClassExaminationSubmit>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import ImportExcelAnalysisModal from './modules/ImportExcelAnalysisModal'
import Vue from 'vue'
import { ACCESS_TOKEN, TENANT_ID } from '@/store/mutation-types'
import { postAction } from '@api/manage'
import JSelectClassExaminationSubmit from '@views/smart_school/components/modal/JSelectClassExaminationSubmit'

export default {
  name: 'ImportExcelAnalysisList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    ImportExcelAnalysisModal,
    JSelectClassExaminationSubmit
  },
  data() {
    return {
      description: 'import_excel_analysis管理页面',
      title: '成绩解析选项',
      msg: '您好，年级考试为必填项。按照各自年级的考试进行选择(避免类似七年级选八年级的考试)',
      isMulti: true,
      isAll: true,
      queryParam: {
        gradeId: '',
        examinationId: '',
        classId: '',
        examinationPaperId: ''
      },
      //查询条件联动部分定义
      queryParamSelect: {},
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '试卷名称',
          align: 'center',
          dataIndex: 'examinationName'
        },
        {
          title: '班级',
          align: 'center',
          dataIndex: 'className'
        },
        {
          title: '考号',
          align: 'center',
          dataIndex: 'examinationNo'
        },
        {
          title: '姓名',
          align: 'center',
          dataIndex: 'studentName'
        },
        {
          title: '客观题',
          align: 'center',
          dataIndex: 'objectiveExa'
        },
        {
          title: '主观题',
          align: 'center',
          dataIndex: 'subjectiveExa'
        },
        {
          title: '总分',
          align: 'center',
          dataIndex: 'totalExa'
        },
        {
          title: '类型',
          align: 'center',
          dataIndex: 'typeExa'
        },
        {
          title: '题号',
          align: 'center',
          dataIndex: 'topicNum'
        },
        {
          title: '得分',
          align: 'center',
          dataIndex: 'topicValue'
        }
        // {
        //   title: '操作',
        //   dataIndex: 'action',
        //   align:"center",
        //   fixed:"right",
        //   width:147,
        //   scopedSlots: { customRender: 'action' }
        // }
      ],
      url: {
        list: '/importExcelAnalysis/importExcelAnalysis/list',
        analysisExaminationData: '/importExcelAnalysis/importExcelAnalysis/analysisExaminationData',
        delete: '/importExcelAnalysis/importExcelAnalysis/delete',
        deleteBatch: '/importExcelAnalysis/importExcelAnalysis/deleteBatch',
        exportXlsUrl: '/importExcelAnalysis/importExcelAnalysis/exportXls',
        importExcelUrl: 'importExcelAnalysis/importExcelAnalysis/importExcel'

      },
      dictOptions: {},
      superFieldList: []
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
    tokenHeader() {
      let head = { 'X-Access-Token': Vue.ls.get(ACCESS_TOKEN) }
      let tenantid = Vue.ls.get(TENANT_ID)
      if (tenantid) {
        head['tenant-id'] = tenantid
      }
      head['examination-id'] = this.queryParam.examinationId
      head['grade-id'] = this.queryParam.gradeId
      head['class-id'] = this.queryParam.classId
      head['examination-paper-id'] = this.queryParam.examinationPaperId
      return head
    }
  },
  methods: {
    initDictConfig() {
    },
    handleOpenSubmit(){
      this.$refs.modalImportExcelAnalysisList.openModal();
    },
    handleCloseExaminationSubmit(val){
      var that = this;
      if(val.msg == 'submit'){
        this.queryParam = val.data;
        this.$confirm({
          title: '温馨提示！',
          content: (<div>
            <p>您确定要执行这个操作吗？</p>
            <p style='color:red;'>注意：重要操作，分析操作会清理已有考试、试卷对应的学生成绩统计信息，重新进行统计，请谨慎操作！</p>
          </div>),
          onOk(){
            that.analysisExaminationData();
          },
          onCancel(){
            that.queryParam={}
          }
        });

      }
    },
    analysisExaminationData() {
      this.loading = true;
      postAction(this.url.analysisExaminationData, this.queryParam).then(res => {
        if (res.success) {
          this.$message.info(res.message)

        } else {
          this.$message.error(res.message)
        }
        this.queryParam={}
        this.loading = false;
      })
    },
    //更改年级-->选择考试，选择班级
    chgGradeId() {
      if (this.queryParam.gradeId) { //判断不为空
        this.queryParamSelect.examinationIdSelect = 'ach_examination,examination_name,id,del_flag  =0 and iz_open  =1 and grade_id=\'' + this.queryParam.gradeId + '\' order by create_time  desc'
        this.queryParamSelect.classIdSelect = 'cou_class,class_name,id,class_iz_open  =1 and class_iz_delete  = 0 and grade_code=\'' + this.queryParam.gradeId + '\' order by  class_order'
      }
    },
    //更改考试-->选择试卷
    chgExaminationId() {
      if (this.queryParam.examinationId) {
        this.queryParamSelect.examinationPaperIdSelect = 'ach_examination_paper aep,aep.examination_paper_name,aep.id,aep.iz_open =1 and aep.del_flag =0 and aep.examination_id =\'' + this.queryParam.examinationId + '\'  order by aep.examination_paper_name'
      }
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'examinationName', text: '试卷名称' })
      fieldList.push({ type: 'string', value: 'className', text: '班级' })
      fieldList.push({ type: 'string', value: 'examinationNo', text: '考号' })
      fieldList.push({ type: 'string', value: 'studentName', text: '姓名' })
      fieldList.push({ type: 'string', value: 'objectiveExa', text: '客观题' })
      fieldList.push({ type: 'string', value: 'subjectiveExa', text: '主观题' })
      fieldList.push({ type: 'string', value: 'totalExa', text: '总分' })
      fieldList.push({ type: 'string', value: 'typeExa', text: '类型' })
      fieldList.push({ type: 'string', value: 'topicNum', text: '题号' })
      fieldList.push({ type: 'string', value: 'topicType', text: '题类型' })
      fieldList.push({ type: 'int', value: 'izOpen', text: '是否启用(1是启用，0否停用)' })
      fieldList.push({ type: 'string', value: 'tenantId', text: '学校id' })
      fieldList.push({ type: 'int', value: 'delFlag', text: '0表示未删除,1表示删除' })
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>